package com.xci.core.base;

import com.xci.core.helper.Helper;
import com.xci.core.helper.WebHelper;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Api控制器基类
 * @author 吕艳阳
 */
public abstract class WebController extends BaseController {
    /**
     * 页面重定向
     * @param url 重定向的Url地址
     */
    protected String redirect(String url) {
        if (Helper.isEmpty(url)) {
            throw new RuntimeException("请指定重定向的Url地址");
        }
        return "redirect:" + url;
    }

    /**
     * 页面跳转
     * @param url 跳转的Url地址
     */
    protected String forward(String url) {
        if (Helper.isEmpty(url)) {
            throw new RuntimeException("请指定跳转的Url地址");
        }
        return "forward:" + url;
    }

    /**
     * 标记新建记录
     * @param map 模型对象
     */
    protected void createMark(ModelMap map) {
        Helper.createMark(map);
    }

    /**
     * 是否新建记录
     */
    protected boolean isCreate() {
        return Helper.isCreate();
    }

    /**
     * 文件下载
     * @param data 下载的数据
     * @param fileName 文件名
     * @param contentType 文档类型
     */
    protected void download(byte[] data, String fileName, String contentType) {
        //        ByteArrayOutputStream tmp = new ByteArrayOutputStream();
        //        tmp.write(data);
        var response = WebHelper.getResponse();
        response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
        if (Helper.isNotBlank(contentType)) {
            response.setHeader("Content-Type", contentType);
        }
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Expires", "0");
        try {
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
//        Integer contentLength = tmp.size();
        try {
            response.getOutputStream().write(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件下载
     * @param filePath 文件路径
     * @param fileName 文件名
     * @param contentType 文档类型
     */
    protected Object download(String filePath, String fileName, String contentType) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        if (Helper.isNotBlank(contentType)) {
            headers.add("Content-Type", contentType);
        }
        try {
            headers.add("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");

        try {
            FileSystemResource file = new FileSystemResource(filePath);
            return ResponseEntity.ok().headers(headers).contentLength(file.contentLength()).body(new InputStreamResource(file.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}